package com.chengke.novel.cacheManager;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chengke.novel.common.constant.CacheConsts;
import com.chengke.novel.common.constant.DatabaseConsts;
import com.chengke.novel.dao.BookInfoMapper;
import com.chengke.novel.dto.responsParm.BookRankRespDto;
import com.chengke.novel.entity.BookInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 小说排行榜 缓存管理类
 * @Author CK
 * @Date 2023/3/21
 */

@Component
@RequiredArgsConstructor
public class BookRankCacheManager {


    private final BookInfoMapper bookInfoMapper;

    /**
     * 查询小说点击榜列表，并放入缓存中
     */
    @Cacheable(cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
            value = CacheConsts.BOOK_VISIT_RANK_CACHE_NAME)
    public List<BookRankRespDto> listVisitRankBooks() {
        LambdaQueryWrapper<BookInfo> bookInfoQueryWrapper = new LambdaQueryWrapper<>();
        // 根据点击量排序
        bookInfoQueryWrapper.orderByDesc(BookInfo::getVisitCount);
        return listRankBooks(bookInfoQueryWrapper);
    }


    /**
     * 查询小说新书榜列表，并放入缓存中
     */
    @Cacheable(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER,
            value = CacheConsts.BOOK_NEWEST_RANK_CACHE_NAME)
    public List<BookRankRespDto> listNewestRankBooks() {
        LambdaQueryWrapper<BookInfo> bookInfoQueryWrapper = new LambdaQueryWrapper<>();
        // 根据创建时间排序
        bookInfoQueryWrapper.orderByDesc(BookInfo::getCreateTime);
        return listRankBooks(bookInfoQueryWrapper);
    }


    /**
     * 查询小说更新榜列表，并放入缓存中
     */
    @Cacheable(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER,
            value = CacheConsts.BOOK_UPDATE_RANK_CACHE_NAME)
    public List<BookRankRespDto> listUpdateRankBooks() {
        LambdaQueryWrapper<BookInfo> bookInfoQueryWrapper = new LambdaQueryWrapper<>();
        bookInfoQueryWrapper
                .orderByDesc(BookInfo::getUpdateTime);
        return listRankBooks(bookInfoQueryWrapper);
    }

    /**
     * 根据传入的条件查询书籍，封装结果返回
     * @param bookInfoQueryWrapper 条件构造器对象
     * @return List<BookRankRespDto>
     */
    private List<BookRankRespDto> listRankBooks(LambdaQueryWrapper<BookInfo> bookInfoQueryWrapper) {
        bookInfoQueryWrapper
                .gt(BookInfo::getWordCount, 0)
                .last(DatabaseConsts.SqlEnum.LIMIT_30.getSql());
        return bookInfoMapper.selectList(bookInfoQueryWrapper).stream().map(v -> {
            BookRankRespDto respDto = new BookRankRespDto();
            respDto.setId(v.getId());
            respDto.setCategoryId(v.getCategoryId());
            respDto.setCategoryName(v.getCategoryName());
            respDto.setBookName(v.getBookName());
            respDto.setAuthorName(v.getAuthorName());
            respDto.setPicUrl(v.getPicUrl());
            respDto.setBookDesc(v.getBookDesc());
            respDto.setLastChapterName(v.getLastChapterName());
            respDto.setLastChapterUpdateTime(v.getLastChapterUpdateTime());
            respDto.setWordCount(v.getWordCount());
            return respDto;
        }).toList();
    }


}
